﻿using App.Common;
using App.IService;
using App.Model.DTO.PhotoCategory;
using App.Model.DTO.User;
using App.Model;
using App.WebAPI.Utilty.ApiResult;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq.Expressions;
using TencentCloud.Nlp.V20190408.Models;

namespace App.WebAPI.Controllers
{
    [Route("[controller]/[Action]")]
    [ApiController]
    [Authorize]
    public class PhotoCategoryController : ControllerBase
    {
        private readonly ILogger<PhotoCategoryController> _logger;
        private readonly IPhotoCategoryService _iPhotoCategoryService;
        public PhotoCategoryController(IPhotoCategoryService iPhotoCategoryService, ILogger<PhotoCategoryController> logger)
        {
            _iPhotoCategoryService = iPhotoCategoryService;
            _logger = logger;
        }
        /// <summary>
        /// 获取分类列表
        /// </summary>
        /// <returns></returns>

        [AllowAnonymous] // 添加此特性允许匿名访问
        [HttpGet]
        public async Task<ApiResult> GetList()
        {
            var types = await _iPhotoCategoryService.QueryAsync();
            if (types.Count == 0) return ApiResultHelper.Error("没有更多分类");
            return ApiResultHelper.Success(types,true);
        }

        /// <summary>
        /// 分类分页查询
        /// </summary>
        /// <param name="iMapper"></param>
        /// <param name="req"></param>
        /// <returns></returns>
        [AllowAnonymous] // 添加此特性允许匿名访问
        [HttpPost]
        public async Task<ApiResult> GetPageList([FromServices] IMapper iMapper, [FromBody] PageRequest req)
        {
            RefAsync<int> total = 0;
            Expression<Func<PhotoCategory, bool>> func = u => true;
            List<PhotoCategory> category = null;
            if (!string.IsNullOrEmpty(req.Filter))
            {
                func = u => u.Name.Contains(req.Filter) || u.Description.Contains(req.Filter);
            }
            if (req.PageIndex == 1 && req.PageSize == int.MaxValue)
            {
                var allPhotoCategory = func != null ? await _iPhotoCategoryService.QueryAsync(func) : await _iPhotoCategoryService.QueryAsync();
                total = category.Count;
                category = allPhotoCategory;
            }
            else
            {
                category = await _iPhotoCategoryService.QueryAsync(func ?? (u => true), req.PageIndex, req.PageSize, total);
            }
            try
            {
                var photoCategoryDTO = iMapper.Map<List<PhotoCategoryDTO>>(category);
                return ApiResultHelper.Success(photoCategoryDTO, total, true);
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("服务器异常");
            }
        }
        /// <summary>
        /// 创建分类
        /// </summary>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Create(CreatePhotoCategoryRequestInput req)
        {
            var id = GuidCombGenerator.GenerateCombGuid32();
            #region 数据验证
            if (String.IsNullOrWhiteSpace(req.Name)) return ApiResultHelper.Error("分类名称不能为空");
            #endregion
            PhotoCategory category = new PhotoCategory
            {
                Id = id,
                Name = req.Name,
                Description = req.Description,
                CoverImage = req.CoverImage,
                CreateAt = DateTime.Now,
                UpDateAt = DateTime.Now,
            };
            bool b = await _iPhotoCategoryService.CreateAsync(category);
            if (!b) return ApiResultHelper.Error("添加失败，服务器异常");
            return ApiResultHelper.Success(category,b);
        }
        /// <summary>
        /// 编辑分类
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Edit(EditPhotoCategoryRequestInput req)
        {
            var category = await _iPhotoCategoryService.FindAsync(req.Id);
            if (category == null) return ApiResultHelper.Error("没有找到该分类");
            category.Name = req.Name;
            category.Description = req.Description;
            category.CoverImage = req.CoverImage;
            bool b = await _iPhotoCategoryService.EditAsync(category);
            if (!b) return ApiResultHelper.Error("修改失败，服务器异常");
            return ApiResultHelper.Success(category,b);
        }
        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Delete(DeletePhotoCategoryRequestInput req)
        {
            bool b = await _iPhotoCategoryService.DeleteAsync(req.Id);
            if (!b) return ApiResultHelper.Error("删除失败，服务器异常");
            return ApiResultHelper.Success(null,b);
        }
    }
}
